package common.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import com.rlyy.aibase.utils.helpers.JsonHelper;
import com.rlyy.aibase.view.key.APIKey_Enum;
import com.rlyy.aibase.view.key.SPEARKey_Enum;
import common.entity.TableData;
import common.util.result.ResultCode;

import java.util.Collection;

public class BoxUtil {

    public static Box successBox(Box in) {
        return createBoxWithTid(in).setOK().setMsg("成功");
    }

    public static Box successBox(Box in, String msg) {
        return createBoxWithTid(in).setOK().setMsg(msg);
    }

    public static Box successBoxToData(Box in, String msg, TableNode tableNode) {
        return successBox(in, msg).putTableNode("data", tableNode);
    }

    public static Box successBoxToData(Box in, Object data) {
        return successBoxToData(in, null, data);
    }

    public static Box successBoxToTableData(Box in, Collection<?> data) {
        return successBoxToTableData(in, data, null);
    }

    public static Box successBoxToTableData(Box in, Collection<?> data, Object total) {
        return successBoxToData(in,null,JsonHelper.json2MapNode(JSONObject.toJSONString(new TableData(data, total), GlobalFastJsonConfig.getSnakeCase())));
    }

    public static Box successBox(Box in, String msg, String kName, String value) {
        MapNode map = new MapNode().putNode(kName, value);
        return createBoxWithTid(in).setOK().setMsg(msg).putMapNode("data", map);
    }

    public static Box warnsBox(Box in, String msg) {
        return createBoxWithTid(in).setMsg(msg).putNode(APIKey_Enum.STATUS.getKey(), ResultCode.Warn);
    }

    public static Box successBoxToData(Box in, String msg, MapNode mapNode) {
        return successBox(in, msg).putMapNode("data", mapNode);
    }

    public static Box successBoxToData(Box in, String msg, Object data) {
        return successBoxToData(in, msg, JsonHelper.json2MapNode(JSONObject.toJSONString(data, GlobalFastJsonConfig.getSnakeCase(), SerializerFeature.DisableCircularReferenceDetect)));
    }

    public static Box successBox(Box in, String msg, TableNode table) {
        return putTableNode(successBox(in, msg), "data", table);
    }

    public static Box putTableNode(Box in, String key, TableNode entryList) {
        String sepLeft = SPEARKey_Enum.SEPAR.getSepar();
        String sepRight = SPEARKey_Enum.SEPAR.getSepar();
        in.toMap().put(sepLeft + key + sepRight, entryList.toList());
        return in;
    }

    public static Box errorBox(Box in, String msg) {
        return createBoxWithTid(in).setError(msg);
    }

    public static Box createBoxWithTid(Box in) {
        return new Box().putNode(APIKey_Enum.TID.getKey(), getTid(in));
    }

    public static String getTid(Box in) {
        return in.getNodeDef(APIKey_Enum.TID.getKey(), null);
    }

    /**
     * 生成 _data_ 使用
     *
     * @param key
     */
    public static String getWith_Key(String key) {
        return SPEARKey_Enum.SEPAR.getSepar() + key + SPEARKey_Enum.SEPAR.getSepar();
    }
}
